package com.company;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author zhf
 * @date 2022/2/22
 */
//给你链表的头结点 head ，请将其按 升序 排列并返回 排序后的链表 。
public class SortList {
    public ListNode sortList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        int n = 0;
        ListNode root = head;
        //计算链表长度
        while (root.next != null){
            n++;
            root = root.next;
        }
        List<Integer> list = new ArrayList<>();
        root = head;
        for (int i = 0; i <= n; i++) {
            list.add(root.val);
            root = root.next;
        }
        //给这个列表排序；
        list.sort(Comparator.comparing(Integer::intValue));
        root = head;
        for (int i = 0; i <= n; i++) {
            root.val = list.get(i);
            root = root.next;

        }
        return root;
    }
}
